﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.NetWork;

namespace HR60_PanelSoft.View
{

    //GC: [g1]设备设置窗口
    //GC: [g2]bug#2623
    public partial class WindowSetDev : Form
    {
        private int _unitSn, _roomSn, _netId, _panelId;
        private HR60_PanelSoft.NetWork.Dto.GateWayDto _gw = new NetWork.Dto.GateWayDto();
        private NetService _netS = new NetService();
        public WindowSetDev()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="unitSn"></param>
        /// <param name="roomSn"></param>
        /// <param name="netId"></param>
        /// <param name="panelId"></param>
        /// <param name="gateWay"></param>
        public void SetProperties(int unitSn, int roomSn, int netId, int panelId, HR60_PanelSoft.NetWork.Dto.GateWayDto gateWay)
        {
            _unitSn = unitSn;
            _roomSn = roomSn;
            _netId = netId;
            _panelId = panelId;
            _gw = gateWay;
        }

        private void RADIO_ONSETDEV_BYCOM_CheckedChanged(object sender, EventArgs e)
        {
            this.CBOX_ONSETDEV_COMLIST.Enabled = this.RADIO_ONSETDEV_BYCOM.Checked;
        }

        private void CBOX_ONSETDEV_COMLIST_DropDown(object sender, EventArgs e)
        {
            string[] tempComs = System.IO.Ports.SerialPort.GetPortNames();
            if (tempComs.Length < 1)
                return;
            this.CBOX_ONSETDEV_COMLIST.Items.Clear();
            foreach (string s in tempComs)
            {
                this.CBOX_ONSETDEV_COMLIST.Items.Add(s);
            }
        }

        private void BTN_ONSETDEV_SEND_Click(object sender, EventArgs e)
        {
            if (RADIO_ONSETDEV_BYGW.Checked)
            {
                try
                {
                    _netS.setDeviceByGateWay(_unitSn, _roomSn, _netId, _panelId, _gw);
                    View.ViewUtils.showMessage("设置成功！");
                }
                catch(Exception ee)
                {
                    View.ViewUtils.showError("设置失败：\r\n" + ee.Message);
                }
                finally
                {
                    
                }
            }
            else
            {
                try
                {
                    if (this.CBOX_ONSETDEV_COMLIST.Text == string.Empty)
                    {
                        View.ViewUtils.showAlert("请选择串口！");
                        return;
                    }
                    _netS.setDeviceByCom(_unitSn, _roomSn, _netId, _panelId, this.CBOX_ONSETDEV_COMLIST.Text);
                    View.ViewUtils.showMessage("设置成功！");
                }
                catch (Exception ee)
                {
                    View.ViewUtils.showError("设置失败：\r\n" + ee.Message);
                }
                finally
                {

                }
            }
        }


        /* [g2.s1]bug2623，增加窗口显示事件 */
        private void WindowSetDev_Shown(object sender, EventArgs e)
        {
            this.TBOX_ONATTRS_UNIT.Text = _unitSn.ToString();
            this.TBOX_ONATTRS_ROOM.Text = _roomSn.ToString();
            this.TBOX_ONATTRS_NET.Text = _netId.ToString();
            this.TBOX_ONATTRS_PNL.Text = _panelId.ToString();
        }

        /* end of [g2.s1] */

        /* [g2.s2]bug2623, 增加属性保存按钮 */
        private void BTN_ONATTRS_SAVE_Click(object sender, EventArgs e)
        {
            int tempUnit = -1, tempRoom = -1, tempNet = -1, tempPnl = -1;

            if (!int.TryParse(TBOX_ONATTRS_UNIT.Text, out tempUnit))
            {
                View.ViewUtils.showAlert("单元号请填写正整数！");
                return;
            }
            else if (!int.TryParse(TBOX_ONATTRS_ROOM.Text, out tempRoom))
            {
                View.ViewUtils.showAlert("门牌号请填写正整数！");
                return;
            }
            else if (!int.TryParse(TBOX_ONATTRS_NET.Text, out tempNet))
            {
                View.ViewUtils.showAlert("网络号请填写正整数！");
                return;
            }
            else if (!int.TryParse(TBOX_ONATTRS_PNL.Text, out tempPnl))
            {
                View.ViewUtils.showAlert("面板号请填写正整数！");
                return;
            }
            _unitSn = tempUnit;
            _roomSn = tempRoom;
            _netId = tempNet;
            _panelId = tempPnl;

        }

        /* end of [g2.s2] */
    }
}
